<template>
	<el-dialog :close-on-click-modal="false" :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="审核状态:" prop="status">
				<el-radio-group v-model="form.status" size="small">
					<el-radio label="2">审核通过</el-radio>
					<el-radio label="3">审核拒绝</el-radio>
				</el-radio-group>
			</el-form-item>
			<template v-if="form.status == 2">
				<el-form-item label="打款凭证" prop="paymentCertificate">
					<scUploadMultiple v-model="form.paymentCertificate" title="打款凭证" :limit="3"></scUploadMultiple>
				</el-form-item>
			</template>
			<template v-if="form.status == 3">
				<el-form-item label="拒绝原因:" prop="statusRemarks">
					<el-input type="textarea" :rows="3" v-model="form.statusRemarks" placeholder="请输入拒绝原因" :maxlength="100" show-word-limit />
				</el-form-item>
			</template>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">确 认</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "audit",
			titleMap: {
				audit: '提现审核'
			},
			visible: false,
			isSaveing: false,
			form: {
				id: null,
				status: '2',
				statusRemarks: "",
				paymentCertificate: []
			},
			rules: {
				status: [
					{ required: true, message: '请选择审核意见', trigger: 'blur' },
				],
				paymentCertificate: [
					{ required: true, message: '请上传打款凭证', trigger: 'blur' },
				],
				statusRemarks: [
					{ required: true, message: '请输入拒绝原因', trigger: 'blur' },
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode) {
			if (mode) {
				this.mode = mode
			}
			this.visible = true
			return this
		},
		setField(key, val) {
			this.form[key] = val
			return this
		},
		async submit() {
			if (!await this.$refs.dialogForm.validate().catch(() => { return false })) {
                return false
            }

			let _data = {
				id: this.form.id,
				status: this.form.status,
				statusRemarks: this.form.statusRemarks,
				paymentCertificate: this.form.paymentCertificate
			}

            let _img = []
            if (_data.paymentCertificate && typeof(_data.paymentCertificate) === 'string') {
                _data.paymentCertificate.split(",").forEach(item => {
                    item && _img.push(item.match(/.+\.com(.+)/)[1])
                })
            } else if(_data.paymentCertificate.length > 0) {
                _data.paymentCertificate.forEach((item) => {
                    _img.push(item.path)
                })
            }
            _data.paymentCertificate = _img

			if (_data.status != 2) {
				delete _data.paymentCertificate
			}

			try {
				this.isSaveing = true
				await this.$API.finance.log.status.put(_data).then(() => {
					this.visible = false
					this.$emit('success')
					this.$notify.success('审核成功')
				})
			} catch (err) {
				this.isSaveing = false;
			} finally {
				this.isSaveing = false;
			}
		},
	}
}
</script>

